#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    #XML
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_DeclarationOfReleaseOfDomesticGoodsSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'DeclarationOfReleaseOfDomesticGoods'
        
    #Read from inn by EDIFACT order
    DocumentTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':None})
    DocumentReferenceID = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':None})
    DocumentFunctionTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'BGM','1225':None})
    
    BondedAreaTypeCode = inn.get({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':None})
    ReleaseDeclarationQuantity = inn.get({'BOTSID':'UNH'},{'BOTSID':'PAC','7224':None})
    
    out.put({'BOTSID':rootelement,ns1+'BondedAreaTypeCode':BondedAreaTypeCode})
    out.put({'BOTSID':rootelement,ns1+'ReleaseDeclarationQuantity':ReleaseDeclarationQuantity})
    
    out.ta_info['botskey'] = 'CUSWBR-5HB-' + DocumentReferenceID.strip()
    out.put({'BOTSID':rootelement,ns1+'DeclarationOfReleaseOfDomesticGoods__{http://www.w3.org/2001/XMLSchema-instance}schemaLocation':'urn:kr:gov:kcs:data:standard:KCS_DeclarationOfReleaseOfDomesticGoodsSchemaModule:1:0:0 ./schema/uncefact/data/KCS_DeclarationOfReleaseOfDomesticGoodsSchemaModule_1.0.0_standard.xsd'})
    
    #Document
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':DocumentTypeCode})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'referenceID':DocumentReferenceID})
    out.put({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'FunctionTypeCode':DocumentFunctionTypeCode})
    
    #Loop 1 ~ 999
    for cts in inn.getloop({'BOTSID':'UNH'},{'BOTSID':'CTS'}):
        ReleaseDeclarationDocumentReferenceID = cts.get({'BOTSID':'CTS','C246.7361':None})
        ReleaseDeclarationGoodsDescription = cts.get({'BOTSID':'CTS'},{'BOTSID':'FTX','4451':'AAA','C108.4440#1':None})
        ReleaseDeclarationGoodsDescription2 = cts.get({'BOTSID':'CTS'},{'BOTSID':'FTX','4451':'AAA','C108.4440#2':None})
        ReleaseDeclarationGoodsDescription3 = cts.get({'BOTSID':'CTS'},{'BOTSID':'FTX','4451':'AAA','C108.4440#3':None})
        ReleaseDeclarationGoodsDescription4 = cts.get({'BOTSID':'CTS'},{'BOTSID':'FTX','4451':'AAA','C108.4440#4':None})
        if ReleaseDeclarationGoodsDescription2 != None: ReleaseDeclarationGoodsDescription = ReleaseDeclarationGoodsDescription + ' ' + ReleaseDeclarationGoodsDescription2
        if ReleaseDeclarationGoodsDescription3 != None: ReleaseDeclarationGoodsDescription = ReleaseDeclarationGoodsDescription + ' ' + ReleaseDeclarationGoodsDescription3 
        if ReleaseDeclarationGoodsDescription4 != None: ReleaseDeclarationGoodsDescription = ReleaseDeclarationGoodsDescription + ' ' + ReleaseDeclarationGoodsDescription4
        ReleaseReason = cts.get({'BOTSID':'CTS'},{'BOTSID':'FTX','4451':'ACD','C108.4440#1':None})
        ReleaseReason2 = cts.get({'BOTSID':'CTS'},{'BOTSID':'FTX','4451':'ACD','C108.4440#2':None})
        if ReleaseReason2 != None: ReleaseReason = ReleaseReason + ' '+ ReleaseReason2
        ReleaseDeclarationReleaseDate = cts.get({'BOTSID':'CTS'},{'BOTSID':'DTM','C507.2005':'261','C507.2380':None})
        
        ReleaseDeclarationPackagingTypeCode = cts.get({'BOTSID':'CTS'},{'BOTSID':'MEA','6311':'CT','C174.6411':None})
        ReleaseDeclarationPackagingQuantity = cts.get({'BOTSID':'CTS'},{'BOTSID':'MEA','6311':'CT','C174.6414':None})
        ReleaseDeclarationReleaseMeasure = cts.get({'BOTSID':'CTS'},{'BOTSID':'MEA','6311':'WT','C174.6414':None})
        
        ConsignorPartyCompanyName = cts.get({'BOTSID':'CTS'},{'BOTSID':'NAD','3035':'GO','C058.3124#1':None})
        ConsignorPartyName = cts.get({'BOTSID':'CTS'},{'BOTSID':'NAD','3035':'GO','C058.3124#2':None})
        ConsignorPartyCommunicationAddressID = cts.get({'BOTSID':'CTS'},{'BOTSID':'NAD','3035':'GO','C058.3124#3':None})
        ConsignorPartyCommunicationAddressID2 = cts.get({'BOTSID':'CTS'},{'BOTSID':'NAD','3035':'GO','C058.3124#4':None})
        ConsignorPartyCommunicationAddressID3 = cts.get({'BOTSID':'CTS'},{'BOTSID':'NAD','3035':'GO','C058.3124#5':None})
        if ConsignorPartyCommunicationAddressID2 != None: ConsignorPartyCommunicationAddressID = ConsignorPartyCommunicationAddressID + ' ' + ConsignorPartyCommunicationAddressID2
        if ConsignorPartyCommunicationAddressID3 != None: ConsignorPartyCommunicationAddressID = ConsignorPartyCommunicationAddressID + ' ' + ConsignorPartyCommunicationAddressID3
        
        ReleaseGoodsTypeCode = cts.get({'BOTSID':'CTS'},{'BOTSID':'RFF','C506.1153':'ACD','C506.1154':None})
        PreviousCustomsDocumentReferenceID = cts.get({'BOTSID':'CTS'},{'BOTSID':'RFF','C506.1153':'ABT','C506.1154':None})
        
        lou = out.putloop({'BOTSID':rootelement,'BOTSID':ns1+'ReleaseDeclaration'})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration',ns2+'ReleaseReason':ReleaseReason})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration',ns2+'ReleaseDate':utils.isoDateFormat(ReleaseDeclarationReleaseDate)})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration',ns2+'ReleaseMeasure':ReleaseDeclarationReleaseMeasure})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration',ns2+'ReleaseMeasure__measureUnitCode':'KG'})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration',ns2+'ReleaseGoodsTypeCode':ReleaseGoodsTypeCode})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'Document',ns2+'referenceID':ReleaseDeclarationDocumentReferenceID})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'Goods',ns2+'Description':ReleaseDeclarationGoodsDescription})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'ReleaseGoodsPackaging',ns2+'Quantity':ReleaseDeclarationPackagingQuantity})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'ReleaseGoodsPackaging',ns2+'TypeCode':ReleaseDeclarationPackagingTypeCode})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'Consignor'},{'BOTSID':ns2+'Party',ns2+'Name':ConsignorPartyName})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'Consignor'},{'BOTSID':ns2+'Party'},{'BOTSID':ns2+'Communication',ns2+'AddressID':ConsignorPartyCommunicationAddressID})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'Consignor'},{'BOTSID':ns2+'Party'},{'BOTSID':ns2+'Communication',ns2+'TypeCode':'02'})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'Consignor'},{'BOTSID':ns2+'Party'},{'BOTSID':ns2+'Company',ns2+'Name':ConsignorPartyCompanyName})
        lou.put({'BOTSID':ns1+'ReleaseDeclaration'},{'BOTSID':ns2+'PreviousCustomsDocument',ns2+'referenceID':PreviousCustomsDocumentReferenceID})
    #End Loop
